package com.newbee.activity.tab;

import java.util.List;

import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.hhu.newbee.R;
import com.newbee.activity.LoginActivity;
import com.newbee.activity.MultChatResultListActivity;
import com.newbee.activity.RequestFriendsListActivity;
import com.newbee.activity.RequestMultChatListActivity;
import com.newbee.app.OnRefrushActivity;
import com.newbee.app.SpeechApp;
import com.newbee.listbean.FriendsAskBean;
import com.newbee.listbean.InviteMultBean;
import com.newbee.service.MainService;
import com.newbee.service.TaskType;
import com.newbee.sqlite.DBHelper;
import com.readystatesoftware.viewbadger.BadgeView;

public class ConfigActivity extends Activity implements OnRefrushActivity,
		OnClickListener
{

	private View mDynamicState;
	private View mAskFriend;

	private View mVoiceRecordFile;
	private View mQunZu;

	private TextView mAskFriendTextView;

	private DBHelper dbHelper;

	private int mInviteMultChatMumb = 1;

	private int mAddFriendMumb = 1;

	private LinearLayout mInviteChatConterner;
	private TextView mLuYinDongTai;
	private BadgeView mInviteMultCharBadgeView;
	private BadgeView mAddFriendBadgeView;

	private LinearLayout mAddFriendContener;

	private TextView mEmail;
	private TextView mNickName;
	private ImageView mHead;

	@Override
	protected void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);
		this.setContentView(R.layout.activity_config);
		MainService.addActivityMap(ConfigActivity.class.getName(), this);
		dbHelper = new DBHelper(this);
		Log.d("WCH",
				"about me activity----------------------------------->init");
		initLayout();
		initFriendView();
		initLuYinDongTai();
		SpeechApp.add(this);
		initUser();

	}

	private void initUser()
	{
		SharedPreferences sp = this.getSharedPreferences(
				LoginActivity.SP_USER_INFO, Activity.MODE_MULTI_PROCESS);

		String email = sp.getString(LoginActivity.SP_KEY_EMAIL, "");
		String head = sp.getString(LoginActivity.SP_KEY_HEAD, "0");
		String nickName = sp.getString(LoginActivity.SP_KEY_NICKNAME, "");
		String userName = sp.getString(LoginActivity.SP_KEY_USERNAME, "");
		mNickName.setText("昵称：" + userName);
		mEmail.setText("邮箱：" + email);
		mHead.setImageDrawable(this.getResources().getDrawable(
				getHeadDrawableID(head)));

	}

	public static int getHeadDrawableID(String str)
	{

		int id = Integer.parseInt(str);

		switch (id)
		{
		case 0:
			return R.drawable.h1;
		case 1:
			return R.drawable.h2;
		case 2:
			return R.drawable.h3;
		case 3:
			return R.drawable.h4;
		case 4:
			return R.drawable.h5;
		case 5:
			return R.drawable.h6;
		case 6:
			return R.drawable.h7;
		case 7:
			return R.drawable.h8;
		case 8:
			return R.drawable.h9;
		case 9:
			return R.drawable.h10;
		case 10:
			return R.drawable.h11;
		case 11:
			return R.drawable.h12;
		}
		return R.drawable.h1;
	}

	private void initLayout()
	{

		mDynamicState = findViewById(R.id.aboutma_ask_for_lunyin);
		mDynamicState.setOnClickListener(this);
		mAskFriend = this.findViewById(R.id.aboutme_ask_for_friend);
		mAskFriend.setOnClickListener(this);
		// mVoiceRecordFile = this.findViewById(R.id.aboutme_luyiin_file);
		mVoiceRecordFile.setOnClickListener(this);

		// mAskFriendTextView = (TextView) this
		// .findViewById(R.id.aboutme_ask_for_friend_text);
		// mInviteChatConterner = (LinearLayout) this
		// .findViewById(R.id.aboutma_ask_for_lunyin_tag);
		// mLuYinDongTai = (TextView) this.findViewById(R.id.luyin_dongtai);
		mAddFriendContener = (LinearLayout) this
				.findViewById(R.id.aboutma_ask_for_addfriend_tag);
		mInviteMultCharBadgeView = new BadgeView(this, mInviteChatConterner);
		mInviteMultCharBadgeView.setText("New!");
		mInviteMultCharBadgeView.setBadgePosition(BadgeView.POSITION_CENTER);
		mAddFriendBadgeView = new BadgeView(this, mAddFriendContener);
		mAddFriendBadgeView.setText("New!");
		mAddFriendBadgeView.setBadgePosition(BadgeView.POSITION_CENTER);
		mEmail = (TextView) this.findViewById(R.id.about_email);
		mNickName = (TextView) this.findViewById(R.id.about_nickname);
		// mHead = (ImageView) this.findViewById(R.id.about_head);

	}

	private void initFriendView()
	{

		List<FriendsAskBean> list = dbHelper.getFriendAsk();

		if (list.size() > 0 && list.get(0).getTag() == DBHelper.UN_SOLVE)
		{
			haveNewsTextViewAskFriend();
		}

	}

	private void initLuYinDongTai()
	{

		List<InviteMultBean> list = dbHelper.getAllInviteMultChat();

		if (list.size() > 0 && list.get(0).getStatue() == DBHelper.UN_SOLVE)
		{
			haveInviteMultChat();
		}

	}

	private void haveNewsTextViewAskFriend()
	{

		mAddFriendBadgeView.show();
	}

	private void haveInviteMultChat()
	{

		mInviteMultCharBadgeView.show();
	}

	@Override
	public void init()
	{
		// TODO Auto-generated method stub

	}

	@Override
	public RtInfo refresh(int taskID, Object... paras)
	{

		Log.d("WCH", "UI----------------------------------->refrush");
		switch (taskID)
		{
		case TaskType.ASK_FOR_FRIEND:
		{
			haveNewsTextViewAskFriend();
			break;
		}
		case TaskType.INVITE_MULT_CHAT:
		{
			haveInviteMultChat();
			break;
		}
		default:
			break;
		}
		return null;

	}

	@Override
	public void onClick(View v)
	{
		switch (v.getId())
		{
		case R.id.aboutme_ask_for_friend:
		{

			mAddFriendBadgeView.hide();
			Intent intent = new Intent(this, RequestFriendsListActivity.class);
			this.startActivity(intent);
			break;
		}
		case R.id.aboutma_ask_for_lunyin:
		{

			mInviteMultCharBadgeView.hide();
			Intent intent = new Intent(this, RequestMultChatListActivity.class);
			this.startActivity(intent);
			break;
		}
		// case R.id.aboutme_luyiin_file:
		// {
		//
		// Intent intent = new Intent(this, MultChatResultListActivity.class);
		// this.startActivity(intent);
		// break;
		// }
		default:
			break;
		}

	}

	public boolean onKeyDown(int keyCode, KeyEvent event)
	{

		if (keyCode == KeyEvent.KEYCODE_BACK)
		{

			SpeechApp.exit();
			return true;
		}
		return super.onKeyDown(keyCode, event);
	}
}
